//----------------------------------------------------------------------------
// Anti-Grain Geometry - Version 2.4
// Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
//
// C# Port port by: Lars Brubaker
//                  larsbrubaker@gmail.com
// Copyright (C) 2007
//
// Permission to copy, use, modify, sell and distribute this software 
// is granted provided this copyright notice appears in all copies. 
// This software is provided "as is" without express or implied
// warranty, and with no claim as to its suitability for any purpose.
//
//----------------------------------------------------------------------------
// Contact: mcseem@antigrain.com
//          mcseemagg@yahoo.com
//          http://www.antigrain.com
//----------------------------------------------------------------------------
//
// classes rbox_ctrl_impl, rbox_ctrl
//
//----------------------------------------------------------------------------
using System;
using System.Collections.Generic;

using AGG.VertexSource;
using AGG.Font;

namespace AGG.UI
{
    //------------------------------------------------------------------------
    public class TextWidget : GUIWidget
    {
        RGBA_Bytes textColor;
		
		StringPrinter printer = new StringPrinter(new StyledTypeFace(LiberationSansFont.Instance, 12), "");
		
        public StringPrinter Printer
        {
            get 
			{ 
				return printer; 
			}
        }

        public TextWidget(string text, double pointSize)
        {
            textColor = RGBA_Bytes.Black;

            Text = text;
            Bounds = printer.Bounds;
        }

        public TextWidget(double x, double y, string text, double pointSize)
        {
            textColor = RGBA_Bytes.Black;

            Text = text;
            Bounds = printer.Bounds;

            Location = new Vector2D(x, y);
        }

        public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                base.Text = value;
                printer.Text = value;
            }
        }

        public override bool InRect(double x, double y)
        {
            Transform.inverse_transform(ref x, ref y);
            return Bounds.hit_test(x,y);
        }

        char[] spaceTrim = { ' ' };

		public override void OnDraw (Graphics2D graphics2D)
		{
            if (Printer.Bounds.Right > Bounds.Right) // only do this if it's static text
            {
                StringPrinter shortTextPrinter = new StringPrinter(Printer.TypeFaceStyle, Printer.Text.Substring(0, Printer.Text.Length-2) + "...");
                while (shortTextPrinter.Bounds.Right > Bounds.Right && shortTextPrinter.Text.Length > 4)
                {
                    shortTextPrinter = new StringPrinter(Printer.TypeFaceStyle, shortTextPrinter.Text.Substring(0, shortTextPrinter.Text.Length - 4).TrimEnd(spaceTrim) + "...");
                }
                graphics2D.Render(shortTextPrinter, textColor);
            }
            else
            {
                // it all fits or it's editable (if editable it will need to be offset/scrolled sometimes).
                graphics2D.Render(Printer, textColor);
            }
			
			base.OnDraw(graphics2D);
		}

        public RGBA_Bytes TextColor
        {
            get
            {
                return textColor;
            }

            set
            {
                textColor = value;
            }
        }
    };
}